import {
	defineStore
} from 'pinia'
import {
	ref,
	computed,
	watchEffect
} from 'vue'
import http from '/utils/request.js'
import {
	useUserStore
} from './user.js'

export const useCartStore = defineStore('cart', () => {
	const cartList = ref([])
	const userStore = useUserStore()

	const userid = userStore.userInfo.userid

	const getCart = async () => {
		try {
			if (!userid) return Promise.resolve()

			const res = await http.post('/cart/list', {
				userid
			})
			if (res.code === '200') {
				cartList.value = res.data
			}
			return Promise.resolve()
		} catch (err) {
			console.error(err)
			return Promise.reject(err)
		}
	}

	const addToCart = async (proid, num) => {
		try {
			if (!userid) return false

			const res = await http.post('/cart/add', {
				userid,
				proid,
				num
			})

			if (res.code === '200') {
				await getCart()
				return true
			}
			return false
		} catch (err) {
			console.error(err)
			throw err
		}
	}

	const updateCartNum = async (cartid, num) => {
		try {
			const res = await http.post('/cart/updatenum', {
				cartid,
				num
			})

			if (res.code === '200') {
				await getCart()
				return true
			}
			return false
		} catch (err) {
			console.error(err)
			throw err
		}
	}

	const deleteCartItem = async (cartid) => {
		try {
			const res = await http.post('/cart/remove', {
				cartid
			})

			if (res.code === '200') {
				await getCart()
				return true
			}
			return false
		} catch (err) {
			console.error(err)
			throw err
		}
	}

	const deleteCartAll = async () => {
		try {
			if (!userid) return false

			const res = await http.post('/cart/removeall', {
				userid
			})

			if (res.code === '200') {
				await getCart()
				return true
			}
			return false
		} catch (err) {
			console.error(err)
			throw err
		}
	}

	const selectCartOne = async (cartid, flag) => {
		try {
			const res = await http.post('/cart/selectone', {
				cartid,
				flag
			})

			if (res.code === '200') {
				await getCart()
				return true
			}
			return false
		} catch (err) {
			console.error(err)
			throw err
		}
	}

	/**
	 * 选中所有
	 * @param{Boolean} type 是否全部选中
	 */
	const selectCartAll = async (type) => {
		try {
			const res = await http.post('/cart/selectall', {
				userid,
				type
			})

			if (res.code === '200') {
				await getCart()
				return true
			}
			return false
		} catch (err) {
			console.error(err)
			throw err
		}
	}

	const getCartNum = computed(() => (proid) => {
		const matchedItem = cartList.value.find(item => item.proid === proid)
		return matchedItem ? matchedItem.num : 0
	});

	const totalCartNum = computed(() => {
		return cartList.value.reduce((total, item) => total + item.num, 0)
	})

	watchEffect(async () => {
		if (userStore.userInfo.userid) {
			await getCart()
			import("/utils/tabBarHelper.js").then(({
				setTabBarBadge
			}) => {
				setTabBarBadge();
			});
		} else {
			cartList.value = [];
		}
	})

	return {
		cartList,
		addToCart,
		updateCartNum,
		deleteCartItem,
		deleteCartAll,
		selectCartOne,
		selectCartAll,
		getCartNum,
		totalCartNum,
		getCart
	}
})